#define LISTBOX_C

#include "App_Debug.h"
#include "LISTBOX.h"
#include "GUI.h"

U8 LISTBOX_CreateEx(LISTBOX_Obj *pObj, char * pListInfo, U8 ListNum, U8 Index)
{
  if(pObj)
  {
    pObj -> pListInfo = pListInfo;
		pObj -> ListNum = ListNum;
		pObj -> Index = Index;
		return 1;
  }

  return 0;
}

/*********************************************************************
*
*       LISTBOX_AddKey
*/
void LISTBOX_AddKey(LISTBOX_Obj* pObj, int Key) {

	if (pObj) {
    switch (Key) {
    case GUI_KEY_DOWN:
	  pObj -> Index ++;
	  if(pObj -> Index >= pObj -> ListNum)
	  	pObj -> Index = 0;
	      
	      break;
	    case GUI_KEY_UP:
	  if(pObj -> Index == 0)
	  	pObj -> Index = pObj -> ListNum - 1;
    else
      pObj -> Index --;
	      break;
    default:
      break;
	    }
	  }
}

void LISTBOX_Set_Index(LISTBOX_Obj *pObj, U8 Index)
{
  if(Index >= pObj -> ListNum)
  {
    ASSERT_FAILED();
	  return;
  }

  pObj -> Index = Index;

}

char *Get_List_Info(LISTBOX_Obj *pObj)
{
  U8 i;
  char *p;

  i = 0;
  p = pObj -> pListInfo;
  while(i < pObj -> Index)
  {
	  if(*p == '\0')
		{
	    i ++;
		}
	
		p ++;
  }

  return p;
}

extern void  Disp_Str1(char Str[], U8 Focus, U8 X, U8 Y);
void LISTBOX_Disp(LISTBOX_Obj* pObj, U8 Focus, U8 X, U8 Y)
{
  char *p;
  
  p = Get_List_Info(pObj);
  Disp_Str1(p, Focus, X, Y);

}
